import { getAdminApi, delAdminApi } from '../../api/admin'
const adminStore = {
  namespaced: true,
  state: {
    adminList: [],//管理员列表数据
    searchText: "",//搜索字段
    pageStart: 1,//初始的页码
    dialogVisible: false,//添加和修改的弹出层
    eidtId: "",//编辑id
    title: "添加管理员",
    btnTxt: "确认添加",
    eidtRow: {},//修改行的数据
    total: 0,//总数据条数
    pageSize: 0,//一页显示多少条
    endPage: 0,//尾页
  },
  mutations: {
    changeEidtId (state, id) { //改变修改id的方法
      state.eidtId = id
    },
    changeEidtRow (state, row) {  //赋值当前修改行数据
      state.eidtRow = row
    },
    changeTitleAndBtn (state, arr) { //改变按钮的文字
      state.title = arr[0];
      state.btnTxt = arr[1];
    },
    changeDialogVisble (state, flag) { //改变弹出框显示隐藏
      state.dialogVisible = flag;
    },
    changeAdminList (state, res) {  //赋值管理列表方法
      if (res.err) {
        state.adminList = [];
      } else {
        state.adminList = res.data.list;
        state.total = res.data.count;//总数据条数
        state.pageSize = res.data.pageSize;
        console.log(Math.ceil(res.data.count / res.data.pageSize))
        state.endPage = Math.ceil(res.data.count / res.data.pageSize)//尾页页码
      }
    },
    changeSearchText (state, text) {//改变搜索字段
      state.searchText = text
    },
    changePage (state, page) {//改变当前页码的方法
      state.pageStart = page;
    },
  },
  actions: {
    async getAdminAction ({ commit }, params) { //获取管理员列表的actions
      var res = await getAdminApi(params)
      console.log(res)
      commit("changeAdminList", res)
      return Promise.resolve(true)
    },
    async delAdminAction (context, tokenId) { //删除管理员的action
      console.log(context)
      var res = await delAdminApi(tokenId)
      console.log(res)
    }
  },
  getters: {
    adminList: state => state.adminList,
    searchText: state => state.searchText,
    pageStart: state => state.pageStart,
    total: state => state.total,
    pageSize: state => state.pageSize,
    endPage: state => state.endPage
  }
}
export default adminStore